package com.example.week8_mybatis_plus.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.week8_mybatis_plus.entity.Goods;
import com.example.week8_mybatis_plus.entity.Supplier;
import com.example.week8_mybatis_plus.service.IGoodsService;
import com.example.week8_mybatis_plus.service.IGoodsSuppliersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jiangbo
 * @since 2022-10-25
 */
@Controller
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    private IGoodsService goodsService;
    @Autowired
    private IGoodsSuppliersService goodsSuppliersService;

    @GetMapping("/{id}")
    public ResponseEntity<Goods> getGoodsById(@PathVariable Integer id) {
        Goods goods = goodsService.getById(id);
        if (goods == null) {
            return ResponseEntity.notFound().build();
        }
        return null;
    }

    @GetMapping("/{id}/suppliers")
    public ResponseEntity<List<Supplier>> getGoodsSuppliers(@PathVariable Integer id) {
        List<Supplier> suppliers = goodsSuppliersService.getGoodsSuppliers(id);
        if (suppliers == null) {
            return ResponseEntity.notFound().build();
        }
        return null;
    }

    @PostMapping("")
    public ResponseEntity<Boolean> addGoodsSuppliers(@RequestBody Goods goods) {
        return (goodsService.addGoods(goods) != null ? ResponseEntity.ok(true) : ResponseEntity.ok(false));
    }

    @GetMapping("")
    public ResponseEntity<Page<Goods>> getGoods(String name,
                                                Double price_max,
                                                Integer number_min,
                                                Integer id,
                                                @RequestParam(defaultValue = "0") Integer pageNum,
                                                @RequestParam(defaultValue = "5") Integer pageSize) {
        Page<Goods> page = new Page<>(pageNum, pageSize);
        Map<String,Object> cond = new HashMap<>();

        cond.put("name", name);
        cond.put("price_max", price_max);
        cond.put("number_min", number_min);
        cond.put("id", id);

        Page<Goods> list = goodsService.getGoods(cond, page);

        if (list.getSize() == 0) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(list);
    }

    @PutMapping("")
    public ResponseEntity<Boolean> updateGoods(@RequestBody Goods goods) {
        return (goodsService.updateGoods(goods) != null ? ResponseEntity.ok(true) : ResponseEntity.ok(false));
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Boolean> deleteGoods(@PathVariable Integer id) {
        return (goodsService.deleteGoods(id) ? ResponseEntity.ok(true) : ResponseEntity.ok(false));
    }

}
